package com.my_test.webmagic.webmagic_modules;

import com.my_test.webmagic.module.entity.*;
import com.my_test.webmagic.module.service.*;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangyalin@act-telecom.com
 * @since 2019-05-27 16:58
 **/
@Component
public class WebMagicMysqlPipeline implements Pipeline {

    @Resource
    private TiebaTypeService tiebaTypeService;
    @Resource
    private TieziService tieziService;
    @Resource
    private UserService userService;
    @Resource
    private FloorService floorService;
    @Resource
    private FloorLoService floorLoService;
    @Resource
    private TiebaService tiebaService;


    @Override
    public void process(ResultItems resultItems, Task task) {

        String url = resultItems.getRequest().getUrl();
        Object list0 = resultItems.get("entityList");
        if (url.matches(WebMagicProcessor.typeRegex)) {
            //类型
            List<TiebaType> list = (List<TiebaType>) list0;
            tiebaTypeService.saveBatch(list);

        } else if (url.matches(WebMagicProcessor.tiebaRegex)) {
            //帖子
            List<Tiezi> list = (List<Tiezi>) list0;
            tieziService.saveBatch(list);

        } else if (url.matches(WebMagicProcessor.tieziRegex)) {
            //楼层
            List<Floor> list = (List<Floor>) list0;
            floorService.saveBatch(list);

            List<User> userList = resultItems.get("userList");
            userService.saveBatch(userList);

        } else if (url.matches(WebMagicProcessor.type2Regex)) {
            //贴吧
            List<Tieba> list = (List<Tieba>) list0;
            tiebaService.saveBatch(list);

        }
    }
}
